package com.hengpeng.msls.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.itfinbase.constants.RespCodeConstants;
import com.hengpeng.itfinbase.constants.TransCodeConstants;
import com.hengpeng.itfinbase.en.ActivityType;
import com.hengpeng.itfinbase.en.RewardType;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.itfinbase.message.activity.CreateShareRedPacketRequest;
import com.hengpeng.itfinbase.persist.Reward;
import com.hengpeng.itfinbase.persist.RewardRedpacket;
import com.hengpeng.msls.util.CommUtil;
/**
 * 微信活动控制器
 * @author weiyuan
 *
 */
@Controller
@RequestMapping("/activityCore")
public class ActivityCoreController extends BaseController {

    /**
     * <pre>
     * 转向获取红包
     * </pre>
     * 
     * @param request
     * @param response
     * @param hostRedPacketId 分享者红包的id
     * @return
     */
    @RequestMapping(value = "/toGetRedPacket")
    public String toGetRedPacket(HttpServletRequest request, HttpServletResponse response, ModelMap map, Long hostRedPacketId) {
        /**
         * 1,获取微信当前登录用户
         * 2，手机号码校验当前用户是否已在本平台注册，已注册红包状态就是可用，未注册红包状态就是未激活
         */
        RewardRedpacket hostRedPacket = serviceFactory.getDaoFactory().getRedPacketDao().get(hostRedPacketId);//获取分享者要分享的红包
        /**
         * 获取分享的六级红包
         */
        RewardType[] rewardTypes = new RewardType[]{RewardType.SHARE_ONE_RED,RewardType.SHARE_TWO_RED,RewardType.SHARE_THREE_RED,
                RewardType.SHARE_FOUR_RED,RewardType.SHARE_FIVE_RED,RewardType.SHARE_SIX_RED};
        List<Reward> sharerewards = serviceFactory.getDaoFactory().getRewardDao().findRewardByTypes(rewardTypes);
        /**
         * 随机生成分享红包金额
         */
        Reward reward = sharerewards.get(CommUtil.getRandom(sharerewards.size(), 0));
        while (true) {
            reward = sharerewards.get(CommUtil.getRandom(sharerewards.size(), 0));
            if (Long.parseLong(reward.getRewardVal()) < hostRedPacket.getMoney()) {
                break;
            }
        }
        map.put("reward", reward);
        map.put("hostRedPacket", hostRedPacket);
        return "/page/shareredpacket/toGetRedPacket.jsp";
    }
    /**
     * 获取红包
     * @param request
     * @param response
     * @param map
     * @param mobile 
     * @param hostRedPacketId
     * @param shareMoney
     * @return
     */
    @RequestMapping(value = "/doGetRedPacket")
    public String doGetRedPacket(HttpServletRequest request, HttpServletResponse response, ModelMap map,
            String mobile,Long hostRedPacketId, Long rewardId) {
        
        CreateShareRedPacketRequest csRequest = new CreateShareRedPacketRequest();
        csRequest.setActivityType(ActivityType.REDPACKET_SHARE);
        csRequest.setMobile(mobile);
        csRequest.setHostRedPacketId(hostRedPacketId);
        csRequest.setRewardId(rewardId);
        csRequest.setTransType(TransCodeConstants.ActivityTransCode.CREATE_SHARE_REDPACKET);
        ResponseMessage responseMessage = serviceFactory.getPortActiveFactory().remoteTrans(csRequest);
        if (RespCodeConstants.CommonCode.TRANS_SUC.equals(responseMessage.getRespCode())) {
            //转向成功页面
        } else {
            //转向失败页面
        }
        return null;
    }
}